package com.dmap.admin.service.backgroundv1.impl;

import com.dmap.admin.controller.background.param.RoleParam;
import com.dmap.admin.dao.AdminRoleMapper;
import com.dmap.admin.po.AdminRole;
import com.dmap.admin.service.backgroundv1.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * \* User: silent
 * \* Date: 08/12/17 Time: 11:02
 * \* Description:
 * \
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private AdminRoleMapper adminRoleMapper;

    @Override
    public List<AdminRole> selectRoleList() {
        return adminRoleMapper.selectByExample(null);
    }

    @Override
    public AdminRole selectRoleById(long roleId) {
        return adminRoleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public AdminRole insert(RoleParam param) {
        if (param == null) {
            return null;
        }
        AdminRole adminRole = new AdminRole();
        adminRole.setName(param.getName());
        adminRole.setDescription(param.getDescription());
        if (param.getParentId() != null) {
            adminRole.setParentId(Long.valueOf(param.getParentId()));
        }
        adminRole.setCreateTime(System.currentTimeMillis());
        adminRole.setModifyTime(adminRole.getCreateTime());
        int success = adminRoleMapper.insertSelective(adminRole);
        return (success == 1) ? adminRole : null;
    }

    @Override
    public AdminRole update(RoleParam param) {
        if (param == null) {
            return null;
        }
        AdminRole adminRole = new AdminRole();
        adminRole.setId(Long.valueOf(param.getId()));
        adminRole.setName(param.getName());
        adminRole.setDescription(param.getDescription());
        if (param.getParentId() != null) {
            adminRole.setParentId(Long.valueOf(param.getParentId()));
        }
        adminRole.setModifyTime(System.currentTimeMillis());
        int success = adminRoleMapper.updateByPrimaryKeySelective(adminRole);
        return (success == 1) ? adminRole : null;
    }

    @Override
    public boolean delete(Long id) {
        int success = adminRoleMapper.deleteByPrimaryKey(id);
        return success == 1;
    }
}
